<?php
session_start();
include("../includes/conn.php");
$dbconn->admin_logincheck();
if($_GET['act']=='del'){
    $id=intval($_GET['id']);
    $dbconn->noretquery("delete from ".DBQIAN."buylog where id=$id");
    header("Location: saleanalyze.php?page=$_GET[page]");
};
$begintime = isset($_GET['begintime'])?$_GET['begintime']:'';
$endtime = isset($_GET['endtime'])?$_GET['endtime']:'';
$where = '1';
if ($begintime)
{
    $where .= ' and '.DBQIAN.'buylog.create_time>=\''.$begintime.'\'';
}
if ($endtime)
{
    $where .= ' and '.DBQIAN.'buylog.create_time<=\''.substr($endtime,0,10).' 23:59:59\'';
}
if ($_GET['act'] == 'trucate')
{
    $dbconn->noretquery("TRUNCATE TABLE ".DBQIAN."buylog");
    header("Location: hyhb.php");
}
echo '<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>欢迎登录后台管理系统</title>
<link href="css/base.css" rel="stylesheet" type="text/css" />
<link href="css/right.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<script language="JavaScript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/jquery-ui-slide.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<script>
$(document).ready(function(){
   $(function(){
   		$(\'.time\').datetimepicker();
      $(\'.rightinfo tbody tr:odd\').css("backgroundColor","#f5f8fa");
   });
});
</script>
</head>

<body style="margin-top:0px;">
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li><a href="index.php">首页</a></li>
    <li>销量分析</li>
  </ul>
</div>
<div class="rightinfo">
  <div class="tools">
    <ul class="toolbar">
    	 <li style="background:#FFF; text-indent:1em; border:0;margin-bottom:10px;">
      <form name="fsoso" method="get" action="">
		开始时间 <input type="text" name="begintime"  value="';echo $_GET['begintime'];;echo '" placeholder="格式：2015-12-01" class="time dfinput" style="width:150px" />
		结束时间 <input type="text" name="endtime"  value="';echo $_GET['endtime'];;echo '" placeholder="格式：2015-12-01" class="time dfinput" style="width:150px" />
      <input name="submit" class="btn" value="查询" type="submit" >
      </form></li><br>
      <li><a onClick="if(confirm(\'您确认要清空吗？\')){location.href=\'?act=trucate\'}" href="#"><span><img src="images/t03.png" /></span>清空记录</a></li>
    </ul>
  </div>
  <br/>
  <p style="height:15px;margin-bottom:10px;"></p>
  <table class="tablelist">
    <thead>
      <tr>
      <th width="3%">序号</th>
        <th width="10%">订单号</th>
        <th width="5%">产品</th>
        <th width="3%">数量</th>
        <th width="5%">产品单价</th>
        <th width="5%">订单总价</th>
        <th width="5%">红包优惠</th>
        <th width="8%">下单时间</th>
        <th width="10%">收货地址</th>
        <th width="5%">收货人</th>
        <th width="8%">联系手机</th>
        <th width="5%">操作</th>
      </tr>
    </thead>
    <tbody>
      ';
$page = ($_GET['page'] == ''||!isset($_GET['page'])) ?1:$_GET['page'];
$pagesize=12;
$num=$dbconn->countn(DBQIAN."buylog",$where);
$pagelist = new page($page,$pagesize,$num ,10,2,0);
$query=$dbconn->news_list(" select * from ".DBQIAN."buylog where ".$where." order by create_time desc ",$page,$pagesize);
$i=1;
while($row=$dbconn->fetchassoc($query)){
    $result[]=$row ;
    echo '<tr height="45">
        <td width="3%" >';echo $i;echo '</td>
        <td width="10%">';echo $row['ordernumber']; echo '</td>
        <td width="5%">';echo $row['product_name']; echo'</td>
        <td width="3%">';echo $row['quantity']; echo'</td>
        <td width="5%">';echo $row['price']; echo'</td>
        <td width="5%">';echo $row['amount']; echo'</td>
        <td width="5%">';echo $row['yh_price']; echo'</td>
        <td width="8%">';echo $row['create_time']; echo'</td>
        <td width="10%">';echo $row['province'].'-'.$row['city'].'-'.$row['address']; echo'</td>
        <td width="5%">';echo $row['name']; echo'</td>
        <td width="8%">';echo $row['tel']; echo'</td>
        <td width="5%">
          <img src="images/t03.png" width="14">
          <a onClick="if(confirm(\'您确认要删除吗？\')){window.location.href=\'?act=del&id=';echo $row['id'];echo '&page=';echo $page;echo '\'}" href="#" class="tablelink">删除</a>
        </td>
      </tr>
          ';
    $i++;
}
foreach ($result as $key => $v) {
    if (!$key) {
        $record[]=array('value' =>$v['quantity'] ,'name'=>$v['product_name']);
    }else{
        foreach ($record as $key => $vc) {
            if ($v['product_name']!=$vc['name']) {
                $ex=1;
            }else{
                $ex=0;
                $record[$key]['value']+=$v['quantity'];
                break;
            }
        }
        if ($ex) {
            $record[]=array('value' =>$v['quantity'] ,'name'=>$v['product_name']);
        }
    }

}
foreach($record as $res){
    $category[]=$res['name'];
    $quantity[]=$res['value'];
}
echo '    </tbody>
  </table>
</div>
<div style=" width:90%; padding:10px 0 10px 0; text-align:center">
  ';if($num!=0) echo $pagelist->showpages();else echo "<font color='#ff0000'>暂无数据</font>";;echo '</div>';
?>
<p style="border:1px solid #00a4ac;" > 销量分析-柱状图</p>
<div id="main" style="height:400px"></div>
<!-- ECharts单文件引入 -->
<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
<script type="text/javascript">
    // 路径配置
    require.config({
        paths: {
            echarts: 'http://echarts.baidu.com/build/dist'
        }
    });
    // 使用
    require(
        [
            'echarts',
            'echarts/chart/pie' // 使用柱状图（bar）、饼状图(pie)相应的模块加载模块，按需加载
        ],
        function (ec) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = ec.init(document.getElementById('main'));
            var option = {
                title : {
                    text: '销量分析',
                    subtext: '（订单）',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                series : [
                    {
                        name: '销量分析',
                        type: 'pie',
                        radius : '65%',
                        center: ['50%', '60%'],
                        data: <?echo json_encode($record);?>,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            // 为echarts对象加载数据
            myChart.setOption(option);
        }
    );
</script>
    <p style="border:1px solid #00a4ac;" > 销量分析-柱状图</p>
    <div id="chart2" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        // 路径配置
        require.config({
            paths: {
                echarts: 'http://echarts.baidu.com/build/dist'
            }
        });
        // 使用
        require(
            [
                'echarts',
                'echarts/chart/bar' // 使用柱状图就加载bar模块，按需加载
            ],
            function (ec) {
                // 基于准备好的dom，初始化echarts图表
                var myChart = ec.init(document.getElementById('chart2'));
                var option = {
                    tooltip: {
                        show: true
                    },
                    legend: {
                        data:['销量(单位:件)']
                    },
                    xAxis : [
                        {
                            type : 'category',
                            data : <?echo json_encode($category);?>
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            "name":"销量(单位:件)",
                            "type":"bar",
                            radius : '65%',
                            center: ['50%', '60%'],
                            "data":<?echo json_encode($quantity);?>,
                        }
                    ]
                };
                // 为echarts对象加载数据
                myChart.setOption(option);
            }
        );
    </script>
<?
echo '</body></html>'; ?>